#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

echo 1 > /proc/sys/net/ipv4/ip_forward
sysctl -w net.ipv4.ip_forward=1 1>/dev/null 2>&1

modprobe ip_tables 2> /dev/null
modprobe ip_nat_ftp 2> /dev/null
modprobe ip_nat_irc 2> /dev/null
modprobe ip_conntrack 2> /dev/null
modprobe ip_conntrack_ftp 2> /dev/null
modprobe ip_conntrack_irc 2> /dev/null

iptables -F
iptables -X
iptables -Z
iptables -F -t nat
iptables -X -t nat
iptables -Z -t nat
iptables -P INPUT   ACCEPT
iptables -P OUTPUT  ACCEPT
iptables -P FORWARD ACCEPT
iptables -t nat -P PREROUTING   ACCEPT
iptables -t nat -P POSTROUTING  ACCEPT
iptables -t nat -P OUTPUT       ACCEPT

# For M11YServer only, This is for squid transparent proxy
#iptables -t nat -A PREROUTING -i eth1 -p tcp ! -d 10.0.0.0/8 --dport 80 -j REDIRECT --to-port 3128

# Route all packet to eth0 by default
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

# setup 10 route for lanxpc
[ -f /etc/openvpn/server1.conf ] && \
iptables -t nat -A POSTROUTING -d 10.0.0.0/8 -o tun0 -j MASQUERADE

# setup 10/15/16 route for lansff
[ -f /etc/openvpn/server4.conf ] && \
iptables -t nat -A POSTROUTING -d 10.0.0.0/8 -o tun0 -j MASQUERADE && \
iptables -t nat -A POSTROUTING -d 15.0.0.0/8 -o tun0 -j MASQUERADE && \
iptables -t nat -A POSTROUTING -d 16.0.0.0/8 -o tun0 -j MASQUERADE

exit 0
